package com.maxd.controller.selfOrder;

import com.maxd.bean.order.DeductPointRes;
import com.maxd.bean.order.PointPayReq;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Orders;
import com.maxd.service.selfOrder.OrdersService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.excel.ExcelData;
import com.maxd.utils.excel.ExportExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@RestController
@Api(value="自营商城订单",tags={"自营商城订单管理"})
@RequestMapping(value = "/orders")
public class OrdersController {
    @Autowired
    private OrdersService service;

    @GetMapping("/list")
    @ApiOperation("后台管理列表")
    public Result findAll(Integer page, Integer size,
                          @ApiParam("订单号")@RequestParam(required = false) String orderNum,
                          @ApiParam("订单状态：1待付款 2已付款 3已发货 4已收货 5已取消 6退款中 7已退款 8拒绝退款")@RequestParam String status,
                          @ApiParam("订单类型：0全部 1普通订单 2拼团订单 3秒杀订单 4积分订单 5保证金订单")@RequestParam Integer orderType,
                          @ApiParam("商品名称")@RequestParam(required = false) String title,
                          @ApiParam("手机号")@RequestParam(required = false) String mobile,
                          @ApiParam("店铺Id")@RequestParam(required = false) Long storeId,
                          String startTime, String endTime,
                          @ApiParam("3核销商品")@RequestParam(required = false) Integer isExpress) {
        return service.findAll(page, size, orderNum, status, orderType, title, mobile, storeId, isExpress,startTime,endTime);
    }


    @GetMapping("/listExcel")
    @ApiOperation("后台管理列表")
    public void listExcel(
            @ApiParam("订单号")@RequestParam(required = false) String orderNum,
            @ApiParam("订单状态：1待付款 2已付款 3已发货 4已收货 5已取消 6退款中 7已退款 8拒绝退款")@RequestParam String status,
            @ApiParam("订单类型：0全部 1普通订单 2拼团订单 3秒杀订单 4积分订单 5保证金订单")@RequestParam Integer orderType,
            @ApiParam("商品名称")@RequestParam(required = false) String title,
            @ApiParam("手机号")@RequestParam(required = false) String mobile,
            @ApiParam("店铺Id")@RequestParam(required = false) Long storeId,
            @ApiParam("3核销商品")@RequestParam(required = false) Integer isExpress,
            String startTime, String endTime,
            HttpServletResponse response) throws Exception {
        ExcelData data = service.listExcel(orderNum, status, orderType, title, mobile, storeId, isExpress,startTime,endTime);
        ExportExcelUtils.exportExcel(response,"订单列表.xlsx",data);
    }





    @GetMapping("/findMyList")
    @ApiOperation("我的订单列表")
    public Result findMyList(Integer page, Integer size,
                             @ApiParam("用户id")@RequestParam String userId,
                             @ApiParam("订单状态：1待付款 2已付款 3已发货 4已收货 5已取消 6退款中 7已退款 8拒绝退款")@RequestParam String status) {
        return service.findMyList(page, size, userId, status);
    }

    @GetMapping("/findMyRefundList")
    @ApiOperation("我的售后订单列表")
    public Result findMyRefundList(Integer page, Integer size,
                             @ApiParam("用户id")@RequestParam String userId) {
        return service.findMyRefundList(page, size, userId);
    }

    @GetMapping("/find")
    @ApiOperation("查询")
    public Result findOne(Long id) {
        return service.findOne(id);
    }

    @PostMapping("/save")
    @ApiOperation("保存订单")
    public Result saveBody(@RequestBody Orders entity) {
        return service.saveBody(entity);
    }

    @PostMapping("/saveAll")
    @ApiOperation("购物车结算多个订单")
    public Result saveBody(@RequestBody List<Orders> list) {
        return service.saveAll(list);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public Result updateBody(@RequestBody Orders entity) {
        return service.updateBody(entity);
    }

    @GetMapping("/delete")
    @ApiOperation("删除")
    public Result delete(Long id) {
        return service.delete(id);
    }

    @ApiOperation(value="取消订单，orderStatus=1时可取消")
    @GetMapping("/cancel")
    public Result orderCancel(@ApiParam("id") @RequestParam Long id){
        return service.orderCancel(id);
    }

    @ApiOperation("后台管理去发货")
    @GetMapping("/express")
    public Result express(Long id, String expressName, String expressNumber){
        return service.express(id, expressName, expressNumber);
    }

    @ApiOperation("后台管理虚拟去发货")
    @GetMapping("/expressVirtual")
    public Result expressVirtual(Long id){
        return service.expressVirtual(id);
    }

    @ApiOperation(value="确认收货，orderStatus=3时收货")
    @GetMapping("/confirm")
    public Result orderConfirm(@ApiParam("id") @RequestParam Long id){
        return service.orderConfirm(id);
    }

    @ApiOperation(value="收入金额统计")
    @GetMapping("/income")
    public Result income(@ApiParam("data") @RequestParam String data, @ApiParam("way： 1年 2月 3日") @RequestParam int way,
                         @ApiParam("店铺Id")@RequestParam(required = false) Long storeId){
        return service.income(data, way, storeId);
    }

    @ApiOperation(value="订单成交量统计")
    @GetMapping("/statistical")
    public Result statistical(@ApiParam("data") @RequestParam String data,
                              @ApiParam("店铺Id")@RequestParam(required = false) Long storeId){
        return service.statistical(data, storeId);
    }

    @ApiOperation("检测是否完成支付")
    @GetMapping("/wxPay")
    public Result wxPayWeb(Long ordersId){
        return service.checkPay(ordersId);
    }

    @ApiOperation("零钱支付")
    @GetMapping("/changePay")
    public Result changePay(String ordersId){
        return service.changePay(ordersId);
    }

    @ApiOperation("积分支付")
    @GetMapping("/jiFenPay")
    public Result jiFenPay(String ordersId){
        return service.jiFenPay(ordersId);
    }

    @ApiOperation("积分支付")
    @PostMapping("/point-pay")
    public Result<Void> pointPay(@RequestBody @Validated PointPayReq req){
        service.pointPay(req);
        return ResultUtil.success();
    }

    @ApiOperation("申请退款")
    @GetMapping("/refund")
    public Result refund(Long ordersId, String refund){
        return service.refund(ordersId, refund);
    }

    @ApiOperation("后台管理确认退款")
    @GetMapping("/refundMoney")
    public Result refundMoney(Long ordersId){
        return service.refundMoney(ordersId);
    }

    @ApiOperation("后台管理拒绝退款")
    @GetMapping("/refusedRefund")
    public Result refusedRefund(Long ordersId, String refusedRefund){
        return service.refusedRefund(ordersId, refusedRefund);
    }

    @ApiOperation("后台管理待处理订单")
    @GetMapping("/pendingOrder")
    public Result pendingOrder(){
        return service.pendingOrder();
    }

    @ApiOperation("用户端提醒发货")
    @GetMapping("/remind")
    public Result remind(Long ordersId){
        return service.remind(ordersId);
    }

    @ApiOperation("后台管理被提醒发货")
    @GetMapping("/remindOrder")
    public Result remindOrder(@ApiParam("商户Id")@RequestParam(required = false)Long merchantId){
        return service.remindOrder(merchantId);
    }

    @ApiOperation("后台管理 用户付款统计")
    @GetMapping("/payCount")
    public Result payCount(Long userId){
        return service.payCount(userId);
    }

    @ApiOperation("用户端 订单统计")
    @GetMapping("/count")
    public Result count(Long userId){
        return service.count(userId);
    }

    @ApiOperation("查快递")
    @GetMapping("/findExpress")
    public Result findExpress(String expressNumber){
        return service.findExpress(expressNumber);
    }

    @ApiOperation("核销商品")
    @GetMapping("/isExpress")
    public Result isExpress(String ids){
        return service.isExpress(ids);
    }

    @ApiOperation("查询用户有效的订单")
    @GetMapping("/user-effect-list")
    public Result<List<Orders>> findEffectByUserId(@RequestParam("userId") Long userId, @RequestParam(value = "typeId", required = false) Long typeId){
        AdminUtils.checkLogin();
        List<Orders> ordersList = service.findEffectByUserId(userId, typeId);
        return ResultUtil.success(ordersList);
    }

    @ApiOperation("查询下单可抵扣积分数")
    @GetMapping("/query-deduct-point")
    public Result<DeductPointRes> queryDeductPoint(@RequestParam("goodsId") Long goodsId){
        DeductPointRes deductPointRes = service.queryDeductPoint(goodsId);
        return ResultUtil.success(deductPointRes);
    }
}
